<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="需求名称" prop="techReqName">
        <el-input
          v-model="queryParams.techReqName"
          placeholder="请输入需求名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" >
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tcsp:techreq:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tcsp:techreq:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tcsp:techreq:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="techreqList" @selection-change="handleSelectionChange" v-if="this.$store.getters.adminShow">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="需求id" align="center" prop="techReqId" />
      <el-table-column label="需求名称" align="center" prop="techReqName" />
      <el-table-column label="发布时间" align="center" prop="pushTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pushTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="需求来源" align="center" prop="source" />
      <el-table-column label="需求所属市州" align="center" prop="municipality" />
      <el-table-column label="联系人" align="center" prop="contactName" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tcsp:techreq:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tcsp:techreq:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>



    <el-row v-if="this.$store.getters.userShow">
      <el-col v-for="(info, techReqId) in techreqList" :key="techReqId" style="margin:10px 0">
        <el-card :body-style="{ padding: '10px'  }" shadow="hover" >
          <div @click="showTechReqDetail(info)">
            <div style="margin:0px 20px">
              <h1>{{info.techReqName}}</h1>
            </div>
            <div class="req-row">
              <span class="req-head">发布时间：</span>
              <span class="req-data">{{info.pushTime}}</span>
            </div>
            <div class="req-row">
              <span class="req-head">需求来源：</span>
              <span class="req-data">{{info.source}}</span>
            </div>
            <div class="req-row">
              <span class="req-head">需求所属市州：</span>
              <span>{{info.municipality}}</span>
            </div>
            <div class="req-row">
              <span class="req-head">项目联系人：</span>
              <span class="req-data">{{info.contactName}}</span>
            </div>
            <div class="req-row">
              <span class="req-head">项目联系人方式：</span>
              <span class="req-data">{{info.contactPhone}}</span>
            </div>
            <div class="req-info">
              <span class="req-head">需求描述：</span>
              <span class="req-data">{{info.reqDetail}}</span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改技术需求对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="需求名称" prop="techReqName">
          <el-input v-model="form.techReqName" placeholder="请输入需求名称" />
        </el-form-item>
        <el-form-item label="需求来源" prop="source">
          <el-input v-model="form.source" placeholder="请输入需求来源" />
        </el-form-item>
        <el-form-item label="需求所属市州" prop="municipality">
          <el-input v-model="form.municipality" placeholder="请输入需求所属市州" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactName">
          <el-input v-model="form.contactName" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="拟投入资金" prop="proposedCap">
          <el-input v-model="form.proposedCap" placeholder="请输入拟投入资金" />
        </el-form-item>
        <el-form-item label="需求详情" prop="reqDetail">
          <el-input v-model="form.reqDetail" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTechreq, getTechreq, delTechreq, addTechreq, updateTechreq } from "@/api/tcsp/techreq";
import store from "@/store";    //用于获取用户角色

export default {
  name: "Techreq",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 技术需求表格数据
      techreqList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        techReqName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        techReqName: [
          { required: true, message: "需求名称不能为空", trigger: "blur" }
        ],
        source: [
          { required: true, message: "需求来源不能为空", trigger: "blur" }
        ],
        municipality: [
          { required: true, message: "需求所属市州不能为空", trigger: "blur" }
        ],
        contactName: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        contactPhone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        proposedCap: [
          { required: true, message: "拟投入资金不能为空", trigger: "blur" }
        ],
        reqDetail: [
          { required: true, message: "需求详情不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询技术需求列表 */
    getList() {
      this.loading = true;
      listTechreq(this.queryParams).then(response => {
        this.techreqList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        techReqId: null,
        techReqName: null,
        source: null,
        municipality: null,
        contactName: null,
        contactPhone: null,
        proposedCap: null,
        reqDetail: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.techReqId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加技术需求";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const techReqId = row.techReqId || this.ids
      getTechreq(techReqId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改技术需求";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.techReqId != null) {
            updateTechreq(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTechreq(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const techReqIds = row.techReqId || this.ids;
      this.$modal.confirm('是否确认删除技术需求编号为"' + techReqIds + '"的数据项？').then(function() {
        return delTechreq(techReqIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tcsp/techreq/export', {
        ...this.queryParams
      }, `techreq_${new Date().getTime()}.xlsx`)
    },
    showTechReqDetail(info) {
      //跳转到详情页
      const params = {}
      this.$tab.openPage("技术需求详情",'/tcsp/techreq-query/query/'+ info.techReqId ,params)
    }
  }
};
</script>

<style scoped>
.req-head {
  font-weight: bold;
}
.req-data {
  color: #727272;
   display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}
.req-row {
  display: inline-block;
  margin: 0px 20px;
}
.req-info {
  margin:0px 20px
}
</style>
